var showMap = function () {
    this.init();
}

showMap.prototype.init = function () {
    var map = this.loadMap();
    var markers = {};
    this.refresh(map,markers);
    setInterval(this.refresh.bind(this,map,markers),5000);
}

showMap.prototype.loadMap = function () {
    var map = new BMap.Map("l-map");
    var point = new BMap.Point(112.907532,28.202142);
    map.centerAndZoom(point, 13);
    map.enableScrollWheelZoom(true);

    var index = 0;
    var myGeo = new BMap.Geocoder();
    function bdGEO(){
        var pt = adds[index];
        geocodeSearch(pt);
        index++;
    }
    function geocodeSearch(pt){
        if(index < adds.length-1){
            setTimeout(window.bdGEO,400);
        }
        myGeo.getLocation(pt, function(rs){
            var addComp = rs.addressComponents;
            document.getElementById("result").innerHTML += index + ". " +adds[index-1].lng + "," + adds[index-1].lat + "："  + "商圈(" + rs.business + ")  结构化数据(" + addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber + ")<br/><br/>";
        });
    }

    return map;
}

showMap.prototype.refresh = function (map,markers) {
    $.ajax({
        "url":"refresh",
        "success":function (data) {
           // var allOverlay = map.getOverlays();
            for (var i = 0; i < data.length ; i++) {
                var text =  data[i].tvalue? data[i].deviceName +','+data[i].tvalue + ',' + data[i].date :data[i].deviceName+ ',没有使用';
                if(markers[data[i].id]){
                    /* 更新设备坐标*/
                    markers[data[i].id].setPosition(new BMap.Point(data[i].latitude, data[i].longitude));
                    markers[data[i].id].setLabel(new BMap.Label(text, {offset: new BMap.Size(20, -10)}));
                    map.addOverlay(markers[data[i].id]);
                }else {
                    /* 增加新设备*/
                    var point = new BMap.Point(data[i].latitude, data[i].longitude);
                    var marker = new BMap.Marker(point);
                    map.addOverlay(marker);
                    marker.setLabel(new BMap.Label(text, {offset: new BMap.Size(20, -10)}));
                    markers[data[i].id] = marker;
                }
            }

            for ( id in markers ) {
                var nodata = true;
                for (var j = 0; j < data.length; j++) {
                    if(id==data[j].id){
                        nodata = false;
                        break;
                    }
                    if(j== (data.length-1) ){
                        map.removeOverlay(markers[id]);
                        markers[id]=null;
                    }
                }
                /* 删除设备*/
                /*if(nodata){
                    map.removeOverlay(markers[id]);
                    markers[id]=null;
                }*/
            }
            /*//var data2 = data;
           /!* for (var i = 0; i < allOverlay.length ; i++) {
                var nodata = true;
                /!* 更新设备坐标*!/
                for (var j = 0; j < data.length; j++) {
                    if (allOverlay[i].id == data[j].id) {
                        var text =  data[j].tvalue? data[j].deviceName + ','+data[j].tvalue + ',' + data[j].date : ',没有使用';
                        allOverlay[i].setPosition(new BMap.Point(data[j].latitude, data[j].longitude));
                        allOverlay[i].setLabel(new BMap.Label( text, {offset: new BMap.Size(20, -10)}));
                        nodata = false;
                        data2[j].id= -1;
                    }
                }
                /!* 删除设备*!/
                if (nodata) {
                    map.removeOverlay(allOverlay[i]);
                }
            }*!/
           /!* 增加新设备*!/
            for (var i = 0; i < data2.length; i++) {
                if(data2[i].id!=-1){
                    var text =  data2[i].tvalue? data2[i].deviceName +  ','+data2[i].tvalue + ',' + data2[i].date : ',没有使用';
                    var point = new BMap.Point(data2[i].latitude, data2[i].longitude);
                    var marker = new BMap.Marker(point);
                    marker.id = data2[i].id;
                    map.addOverlay(marker);
                    marker.setLabel(new BMap.Label(text, {offset: new BMap.Size(20, -10)}));
                }
            }*/
            //$('label').css('max-width','300px');
        },
        'error':function(err) {
            console.log(err);
        }
    });
}

